package com.ysm.smfu.welcome;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ysm.smfu.MainActivity;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.BaseActivity;
import com.ysm.smfu.activity.FoundActivity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.MyImageBitmapUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 广告页
 */
public class AdActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.adImg)
    ImageView adImg;
    @BindView(R.id.adText)
    TextView adText;
    //    延迟秒数
    int delayMills = 3;
    //    上次图片地址
    private String preAdImg;
    //    上次广告链接
    private String preAdLink;
    //    当前图片地址
    private String currAdImg;
    //    当前广告链接
    private String currLink;
    //    真正的广告链接
    private String realLink;
    private String realId;
    private SharedPreferences sp;
    private String preAdId;
    private String currAdId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        }
        ButterKnife.bind(this);
        adText.setText("跳过 " + delayMills);
        adImg.setOnClickListener(this);
        adText.setOnClickListener(this);
        getAdInfo();
        showAdInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        开始倒计时
        handler.sendEmptyMessageDelayed(100, 1000);
    }

    /**
     * 展示广告信息
     */
    private void showAdInfo() {
        Bitmap localAdImg = MyImageBitmapUtil.getBitmapLoc(SpKey.LOCALADIMGADD);
        if (localAdImg != null) {  //先加载本地缓存的上次广告信息
            PrintUtil.printRespones("加载缓存广告");
            adImg.setImageBitmap(localAdImg);
            realLink = preAdLink;
            realId = preAdId;
        }
        //如果当前获取到的图片地址和上次不一样，则加载当前信息
        if (!TextUtils.isEmpty(currAdImg) && !TextUtils.equals(preAdImg, currAdImg)) {
            PrintUtil.printRespones("加载新的广告");
            Glide.with(this).load(currAdImg).into(adImg);
            realLink = currLink;
            realId = currAdId;
        }
        if (localAdImg == null && (TextUtils.isEmpty(currAdImg) || TextUtils.equals(preAdImg, currAdImg))) {
            if (!TextUtils.isEmpty(preAdImg)) {
                Glide.with(this)
                        .load(preAdImg)
                        .skipMemoryCache(true)
                        .into(adImg);
                realLink = preAdLink;
                realId = preAdId;
            } else {
                finishSelf();
            }
        }
    }

    /**
     * 获取广告信息
     */
    private void getAdInfo() {
        sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        preAdImg = sp.getString(SpKey.PREADIMG, "");
        preAdLink = sp.getString(SpKey.PREADLINK, "");
        preAdId = sp.getString(SpKey.PREADID, "");

        currAdImg = sp.getString(SpKey.CURRADIMG, "");
        currLink = sp.getString(SpKey.CURRLINK, "");
        currAdId = sp.getString(SpKey.CURRPREADID, "");
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (delayMills > 1) {
                delayMills--;
                adText.setText("跳过 " + delayMills);
                handler.sendEmptyMessageDelayed(100, 1000);
            } else {
                finishSelf();
            }
        }
    };

    @Override
    public void onClick(View v) {
        handler.removeMessages(100);
        switch (v.getId()) {
            case R.id.adText:
                finishSelf();
                break;
            case R.id.adImg:
                goOnlineAdPage();
                break;
        }
    }

    /**
     * 关闭自己
     */
    private void finishSelf() {
        UIUtils.startActivity(MainActivity.class);
        finish();
    }

    /**
     * 跳往广告详情页
     */
    private void goOnlineAdPage() {
        Intent intent = new Intent(this, FoundActivity.class);
        intent.putExtra("found", 13);
        intent.putExtra("realLink", realLink);
        intent.putExtra("realId", realId);
        startActivity(intent);
        finish();
    }

    /**
     * 广告页点击返回，退出应用
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        handler.removeMessages(100);
        finish();
    }
}
